package com.example.demo.service.impl;

import com.example.demo.mapper.*;
import com.example.demo.entity.ApiRequestResult;
import com.example.demo.entity.ApiTestCaseGroupResult;
import com.example.demo.entity.ApiTestCaseResult;
import com.example.demo.entity.JobPlanLog;
import com.example.demo.entity.response.jobPlan.JobPlanDetailResp;
import com.example.demo.entity.response.jobPlan.JobPlanResp;
import com.example.demo.entity.response.jobPlan.club.GroupResultList;
import com.example.demo.entity.response.jobPlan.club.TestResultList;
import com.example.demo.entity.testData.PageParams;
import com.example.demo.service.JobPlanService;
import com.example.demo.utils.PageInfoNew;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class JobPlanServiceImpl implements JobPlanService {
    @Resource
    JobPlanLogMapper jobPlanLogMapper;
    @Resource
    JobMsgMapper jobMsgMapper;

    @Resource
    ApiRequestResultMapper apiRequestResultMapper;

    @Resource
    ApiTestCaseGroupResultMapper apiTestCaseGroupResultMapper;

    @Resource
    ApiTestCaseResultMapper apiTestCaseResultMapper;


    @Override
    public PageInfoNew<JobPlanResp> page(int pageNum, int pageSize, int projectId) {
        PageHelper.startPage(pageNum, pageSize);
        List<JobPlanResp> jobPlanRespList = new ArrayList<>();
        List<JobPlanLog> list = jobPlanLogMapper.selectAllByCondition(projectId);
        PageInfo<JobPlanLog> pageInfo = new PageInfo<>(list);
        list.forEach(jobPlanLog -> {
            JobPlanResp resp = new JobPlanResp();
            resp.setJobName(jobMsgMapper.selectById(jobPlanLog.getJobId()).getJobName());
            BeanUtils.copyProperties(jobPlanLog, resp);
            jobPlanRespList.add(resp);
        });
        PageParams pageParams = new PageParams();
        pageParams.setPageNum(pageInfo.getPageNum());
        pageParams.setPages(pageInfo.getPages());
        pageParams.setPageSize(pageInfo.getPageSize());
        pageParams.setPageTotal(pageInfo.getTotal());
        pageParams.setSize(pageInfo.getSize());
//        System.out.println(hexDataList);
        return new PageInfoNew<>(jobPlanRespList, pageParams);
    }

    @Override
    public JobPlanDetailResp details(int id) {
        JobPlanLog jobPlanLog = jobPlanLogMapper.selectOne(id);
        JobPlanDetailResp resp = new JobPlanDetailResp();
        List<ApiTestCaseGroupResult> apiTestCaseGroupResults = apiTestCaseGroupResultMapper.getList(jobPlanLog.getId());
        List<GroupResultList> groupResultLists = new ArrayList<>();
        apiTestCaseGroupResults.forEach(apiTestCaseGroupResult -> {
            GroupResultList groupResultList = new GroupResultList();
            BeanUtils.copyProperties(apiTestCaseGroupResult, groupResultList);
            List<ApiTestCaseResult> testCaseResults = apiTestCaseResultMapper.selectList(jobPlanLog.getId(), apiTestCaseGroupResult.getTestCaseGroupId());
            List<TestResultList> testResultLists = new ArrayList<>();
            testCaseResults.forEach(apiTestCaseResult -> {
                TestResultList testResultList = new TestResultList();
                BeanUtils.copyProperties(apiTestCaseResult, testResultList);
                testResultLists.add(testResultList);
            });
            testResultLists.forEach(testResultList -> {
                List<ApiRequestResult> apiRequestResultList = apiRequestResultMapper.selectList(jobPlanLog.getId(), testResultList.getTestCaseId());
                testResultList.setApiRequestResults(apiRequestResultList);
            });
            groupResultList.setTestResultLists(testResultLists);
            groupResultLists.add(groupResultList);
        });

        BeanUtils.copyProperties(jobPlanLog, resp);
        resp.setGroupResultLists(groupResultLists);
        return resp;
    }

    @Override
    public void delete(int id) {
        jobPlanLogMapper.deleteById(id);
    }
}
